package cn.xinyangtong.shopping.portal.controller;

import cn.xinyangtong.facade.rest.ItemService;
import cn.xinyangtong.pojo.TbItem;
import cn.xinyangtong.shopping.portal.pojo.BuyerCart;
import cn.xinyangtong.shopping.portal.pojo.CartItem;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by fengbo on 2017/7/30.
 */
@Controller
@RequestMapping("/cart")
public class CartController {

    private final String BUYER_CART = "BUYER_CART";

    @Autowired
    private ItemService itemService;

    @RequestMapping("/list")
    public String showCart(Model model, HttpServletRequest request) {

        Cookie[] cookies = request.getCookies();
        BuyerCart buyerCart = null;
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                if (BUYER_CART.equalsIgnoreCase(cookie.getName())) {
                    buyerCart = JSON.parseObject(cookie.getValue(), BuyerCart.class);
                    break;
                }
            }
        }
        if (buyerCart == null) {
            buyerCart = new BuyerCart();
        }
        model.addAttribute("buyerCart", buyerCart);
        return "cart";
    }

    @RequestMapping("/addItem")
    public String addItemToCart(Long itemId, Integer amount,
                                HttpServletRequest request, HttpServletResponse response) {
        Cookie[] cookies = request.getCookies();
        BuyerCart buyerCart = null;
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                if (BUYER_CART.equalsIgnoreCase(cookie.getName())) {
                    buyerCart = JSON.parseObject(cookie.getValue(), BuyerCart.class);
                    break;
                }
            }
        }
        if (buyerCart == null) {
            buyerCart = new BuyerCart();
        }
        boolean isExist = false;
        List<CartItem> cartItems = buyerCart.getCartItems();
        if (cartItems != null && cartItems.size() > 0) {
            for (CartItem cart : cartItems) {
                if (cart.getId() == itemId) {
                    cart.setAmount(cart.getAmount() + 1);
                    isExist = true;
                }
            }
        }
        if (!isExist) {
            CartItem cartItem = new CartItem();
            //根据商品id查询商品
            TbItem item = itemService.getItemBaseInfo(itemId);
            cartItem.setId(item.getId());
            cartItem.setTitle(item.getTitle());
            cartItem.setImage(item.getImage() == null ? "":item.getImage().split(",")[0]);
            cartItem.setAmount(amount);
            cartItem.setPrice(item.getPrice());
            buyerCart.getCartItems().add(cartItem);
        }
        Cookie cookie = new Cookie(BUYER_CART, JSON.toJSONString(buyerCart));
        //设置cookie时间
        cookie.setMaxAge(60*60*24);
        //设置路径
        cookie.setPath("/");
        //上线后，申请域名
        response.addCookie(cookie);
        return "redirect:/cart/list";
    }
}
